// pages/csregistration/csregistration.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
var app = getApp();
var isaddimg = false;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        consultid: 0,
        whether: 0,
        type: 1,
        photos: {},
        education: {},
        train: {},
        addtype: 0,
        start_date: '',
        end_date: '',
        school: '',
        major: '',
        educate: '',
        intro: '',
        account_id: 0,
        touch_start: 0,
        touch_end: 0,
        id: 0,
    },
    onLoad: function(option) {
        this.setData({
            type: option.type
        })
    },
    bindstartDateChange: function(e) {
        this.setData({
            start_date: e.detail.value
        })
    },
    bindendDateChange: function(e) {
        this.setData({
            end_date: e.detail.value
        })
    },
    schoolInput: function(e) {
        this.setData({
            school: e.detail.value
        })
    },
    majorInput: function(e) {
        this.setData({
            major: e.detail.value
        })
    },
    educationInput: function(e) {
        this.setData({
            educate: e.detail.value
        })
    },
    addeducate: function(e) {
        this.setData({
            addtype: e.currentTarget.dataset.addtype,
            start_date: '',
            end_date: '',
            school: '',
            major: '',
            educate: '',
            intro: '',
            id: 0
        })
    },
    addtrain: function(e) {
        this.setData({
            addtype: e.currentTarget.dataset.addtype,
            start_date: '',
            end_date: '',
            school: '',
            major: '',
            educate: '',
            intro: '',
            id: 0
        })
    },
    introInput: function(e) {
        this.setData({
            intro: e.detail.value,
        })
    },
    confirm: function() {
        let that = this
        if (that.data.addtype == 1) {
            if (that.data.start_date == '') {
                api.showError('请选择开始时间')
                return false
            }
            if (that.data.end_date == '') {
                api.showError('请选择结束时间')
                return false
            }
            if (that.data.school == '') {
                api.showError('请填写学校')
                return false
            }
            if (that.data.major == '') {
                api.showError('请填写专业')
                return false
            }
            if (that.data.educate == '') {
                api.showError('请填写学位')
                return false
            }
            let data = {
                consultant_id: that.data.consultid,
                start_date: that.data.start_date,
                end_date: that.data.end_date,
                school: that.data.school,
                major: that.data.major,
                educate: that.data.educate,
                account_id: that.data.account_id,
                id: that.data.id
            }
            let url = app.u.HOME_URL + app.u.ADD_EDUCATION
            api.requestUrl(data, url).then(res => {
                if (res.code == 200) {
                    api.showSuccess(res.msg)
                    that.setData({
                        education: res.data
                    })
                    setTimeout(function() {
                        that.zcancel()
                    }, 1000)
                } else {
                    api.showError(res.msg)
                }
            })

        } else if (that.data.addtype == 2) {
            if (that.data.start_date == '') {
                api.showError('请选择开始时间')
                return false
            }
            if (that.data.end_date == '') {
                api.showError('请选择结束时间')
                return false
            }
            if (that.data.intro == '') {
                api.showError('请填写培训机构')
                return false
            }
            let data = {
                consultant_id: that.data.consultid,
                start_date: that.data.start_date,
                end_date: that.data.end_date,
                intro: that.data.intro,
                account_id: that.data.account_id,
                id: that.data.id
            }
            let url = app.u.HOME_URL + app.u.ADD_TRAIN
            api.requestUrl(data, url).then(res => {
                if (res.code == 200) {
                    api.showSuccess(res.msg)
                    that.setData({
                        train: res.data
                    })
                    setTimeout(function() {
                        that.zcancel()
                    }, 1000)
                } else {
                    api.showError(res.msg)
                }
            })

        }
    },
    zcancel: function(e) {
        this.setData({
            addtype: 0,
            start_date: '',
            end_date: '',
            school: '',
            major: '',
            educate: '',
            intro: '',
        })
    },
    adduserphoto: function() {
        isaddimg = true
        let that = this
        let data = {
            consultant_id: that.data.consultid,
            photos: that.data.photos.join(','),
            account_id: that.data.account_id
        }
        let url = app.u.HOME_URL + app.u.ADD_PHOTOS
        api.requestUrl(data, url).then(res => {
            if (res.code == 200) {
                api.showSuccess(res.msg)
            } else {
                api.showError(res.msg)
            }
        })
    },
    mytouchstart: function(e) {
        this.setData({
            touch_start: e.timeStamp
        })
    },
    mytouchend: function(e) {
        this.setData({
            touch_end: e.timeStamp
        })
    },
    addimg: function() {
        isaddimg = true
        api.chooseimage(9, this.data.photos, 'file', app.u.HOME_URL + app.u.UPLOADIMG).then(res => {
            this.setData({
                photos: this.data.photos.concat(res)
            })
            this.adduserphoto()
        })
    },
    giftview: function(e) {
        isaddimg = true
        let touchTime = this.data.touch_end - this.data.touch_start
        if (touchTime > 350) {
            let that = this
            let images = that.data.photos
            var index = e.currentTarget.dataset.index
            wx.showModal({
                title: '提示',
                content: '确定要删除此图片吗？',
                success(res) {
                    if (res.confirm) {
                        images.splice(index, 1)
                        that.setData({
                            photos: images
                        })
                        that.adduserphoto()
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            wx.previewImage({
                current: e.currentTarget.dataset.url,
                urls: this.data.photos
            })
        }
    },
    educateview: function(e) {
        isaddimg = false
        let that = this
        let touchTime = this.data.touch_end - this.data.touch_start
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        if (touchTime > 350) {
            let that = this
            wx.showModal({
                title: '提示',
                content: '确定要删除该教育经历？',
                success(res) {
                    if (res.confirm) {
                        let url = app.u.HOME_URL + app.u.CONSULTANT_DEL
                        let data = { id: id, type: 1, account_id: that.data.account_id }
                        api.requestUrl(data, url).then(res => {
                            if (res.code == 200) {
                                that.setData({
                                    education: res.data
                                })
                            } else {
                                api.showError(res.msg)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            let index = e.currentTarget.dataset.index
            that.setData({
                id: that.data.education[index].id,
                start_date: that.data.education[index].start_date,
                end_date: that.data.education[index].end_date,
                school: that.data.education[index].school,
                major: that.data.education[index].major,
                educate: that.data.education[index].education,
                // intro: that.data.train[index].intro,
                addtype: 1
            })
        }
    },
    trainview: function(e) {
        isaddimg = false
        let that = this
        let touchTime = this.data.touch_end - this.data.touch_start
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        if (touchTime > 350) {
            let that = this
            wx.showModal({
                title: '提示',
                content: '确定要删除培训？',
                success(res) {
                    if (res.confirm) {
                        let url = app.u.HOME_URL + app.u.CONSULTANT_DEL
                        let data = { id: id, type: 2, account_id: that.data.account_id }
                        api.requestUrl(data, url).then(res => {
                            if (res.code == 200) {
                                that.setData({
                                    train: res.data
                                })
                            } else {
                                api.showError(res.msg)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            let index = e.currentTarget.dataset.index
            that.setData({
                id: that.data.train[index].id,
                start_date: that.data.train[index].start_date,
                end_date: that.data.train[index].end_date,
                // school: that.data.train[index].start_date,
                // major: that.data.train[index].start_date,
                // educate: that.data.train[index].start_date,
                intro: that.data.train[index].intro,
                addtype: 2
            })
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (isaddimg) {
            isaddimg = false;
            return;
        }
        let that = this
        let consultid = 0
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                if (res.data.consult) {
                    consultid = res.data.consult.id

                }
                that.setData({
                    account_id: res.data.id
                })
                let data = { id: consultid }
                let url = app.u.HOME_URL + app.u.PSY_OTHER
                api.requestUrl(data, url).then(res => {
                    if (res.code == 200) {
                        that.setData({
                            photos: res.data.photos,
                            education: res.data.education,
                            train: res.data.train,
                            consultid: consultid,
                        })
                    } else {
                        api.showError(res.msg)
                    }
                })
            }
        })
        wx.showShareMenu({
            withShareTicket: true
        })
    }
})